<?php
class ManageLogoForm extends Form
{
    function __construct()
    {           	
        Form::Form("ManageLogoForm");
        
		$this->link_css("style/ui.datepicker.css");
        $this->link_js("javascript/jquery/ui.datepicker.js");
        
        $id = (int) Url::get("id", 0);
        $active = (int)Url::get("active");
        $act = Url::get("act");
        
        if ($act == "delete" && $id)
        {
            $record = DB::select("website_logo", "id=" . $id);

            Upload::storage_delete_file($record["image"], $record["img_server"]);
            $sql = "delete FROM website_logo WHERE id = " . $id;
            DB::query($sql);
            ECLib::getWebsiteLogo(1);
            
            Url::redirect_current();
        }       
        
        if(($active==0 || $active==1) && $id && $act !="edit")
        {
        	$record = DB::select("website_logo", "id=" . $id);
        	
        	if($active==1)
        	{
        		DB::query("UPDATE website_logo SET status=0");
        		
        		if($record["end_date"]<TIME_NOW){
        			Url::redirect_current();
        		}
        	}
        	 
    		DB::update("website_logo",array("status"=>$active)," id = $id");
    		
    		ECLib::getWebsiteLogo(1);
    		
    		Url::redirect_current();
        }       
        
    }
    
    function on_submit()
    {       
    	$logo = array();
    	$error = false;
        $img = $_FILES["uploadLogo"]["name"];
        $img = explode(".", $img);
        $img = array_pop($img);
        $mini = microtime(true);
        $img = $mini . "." . $img;
        
        $start_date = Url::get('logoStartDate', 0);
        $end_date = Url::get('logoEndDate', 0);
        
        $logo['title'] = Url::getParam('logoTitle');
       
        $logo['username'] = User::user_email();        
        $status = Url::get('logoActive', 0);
        $logo['status'] =  $status;
        
        $id = (int)Url::get("id");
        $act = Url::get("act");
        
        if($logo['title']==""){
        	$this->setFormError("msg","Bạn phải nhập tiêu đề cho ảnh<br>");
            $error = true;
        }
        
        if ($start_date)
        {
            $date_arr = explode("-", $start_date);
            if (isset($date_arr[0]) && isset($date_arr[1]) && isset($date_arr[2]))
                $logo['start_date'] = mktime(0, 0, 0, (int) $date_arr[1], (int) $date_arr[0], (int) $date_arr[2]);
        }
        else
        {
        	$logo['start_date'] = 0;
        }

        if ($end_date)
        {
            $date_arr = explode("-", $end_date);

            if (isset($date_arr[0]) && isset($date_arr[1]) && isset($date_arr[2]))
            {
            	$logo['end_date'] = mktime(23, 59, 59, (int) $date_arr[1], (int) $date_arr[0], (int) $date_arr[2]);
            }
        }
        else
        {
        	$logo['end_date'] = 0;
        }
		        		
        if($logo['start_date']==0 || $logo['end_date'] == 0){		
        	$logo['start_date']=0;
        	$logo['end_date'] = 0;
        }        	
		
        if (!isset($logo['img_server']))
        {
        	$logo['img_server'] = IMAGE_SERVER_NO;
        }
		
        if($_FILES["uploadLogo"]["tmp_name"])
        {
            if(Upload::storage_check_dir("logo/", true, IMAGE_SERVER_NO))
            {
                $file_name = "logo/" . $img;
                $sourceName = $_FILES["uploadLogo"]["tmp_name"];
                Upload::storage_put_file($file_name, $sourceName, IMAGE_SERVER_NO);
                $logo["image"] = $file_name;
            }
            
        }
        elseif(!$_FILES["uploadLogo"]["tmp_name"] && $act != "edit"){
        	$this->setFormError("msg","Bạn phải chọn ảnh");
            $error = true;
        }
        if($status==1)
    	{
    		DB::query("UPDATE website_logo SET status=0");
    	}
        
        if($act == "edit" && $id){  
        	DB::update_id("website_logo", $logo, $id);			
            ECLib::getWebsiteLogo(1);
            Url::redirect_current();
        }
        
        if(!$error && isset($logo["image"]) && $logo["image"])
        {
            DB::insert("website_logo", $logo);
            ECLib::getWebsiteLogo(1);
            Url::redirect_current();
        }
    }

    function draw(){
    	
        global $display;		
        $this->beginForm(true);
        
        $items = array();
       	$img_scr = "";
        $id = (int) Url::get("id", 0);
        $act = Url::get("act"); 
        
        if($act == "edit"){
        	$row = DB::select("website_logo", "id=" . $id);
        	
        	$start_date = $row["start_date"]!=0?date("d-m-Y",$row["start_date"]):0;	
        	$end_date = $row["end_date"]!=0?date("d-m-Y",$row["end_date"]):0;	
	        $title = $row["title"];
	        $active = $row["status"];
	        $img_scr = CGlobal::$img_server[$row['img_server']] . $row['image'];
	        
        }
        else{
        	$start_date = Url::get("logoStartDate", "");
	        $end_date = Url::get("logoEndDate", "");
	        $title = Url::get("logoTitle");
	        $active = Url::get("logoActive");
        }
        
		
        $row = array();
        $display->add("title",$title);
        $display->add("start_date",$start_date);
        $display->add("end_date",$end_date);
        $display->add("active",$active);
        $display->add("img_scr",$img_scr);
        $display->add("act",$act);
        
        $sql = "SELECT * FROM website_logo ORDER BY status DESC, start_date DESC, end_date DESC";
		$result = DB::query($sql);
		while ($row = mysql_fetch_assoc($result))
		{
			
			$row["img_src"] = CGlobal::$img_server[$row['img_server']] . $row['image'];
			
			if($row["start_date"]==0 && $row["end_date"]==0)		
			{
				if($row["status"]==1){
					$row["name_status"] = '<div style="color:green">Đang chạy</div>';
				}
				else 
				{
					$row["name_status"] = '<div style="color:red">Tạm dừng</div>';
				}
		    }
			elseif($row["start_date"]!=0 && $row["end_date"]!=0)
			{	
				if($row["status"]==1){	
					if(TIME_NOW>$row["start_date"] && TIME_NOW<$row["end_date"])
					{
						$row["name_status"] = '<div style="color:green">Đang chạy</div>';			
					}
					elseif(TIME_NOW<$row["start_date"])
					{
						$row["name_status"] = '<div><b>Sắp chạy</b></div>';
					}
					elseif(TIME_NOW>$row["end_date"])
					{					
						$row["name_status"] = '<div style="color:red">Tạm dừng</div>';					
						$row["status"] = 0;
					}
								
				}
				else
				{
					$row["name_status"] = '<div style="color:red">Tạm dừng</div>';
				}
			}
			
			
			$row["start_date"] = ($row["start_date"]==0)?"Không giới hạn":ECLib::formatDateLong($row["start_date"]);	
			$row["end_date"] = ($row["end_date"]==0)?"Không giới hạn":ECLib::formatDateLong($row["end_date"]);	
			
			$items[$row["id"]] = $row;
		}
		
        $display->add("items",$items);
                
        $display->add("msg",$this->showFormErrorMessages(1));
        
        $display->output("ManageLogo");
        
        $this->endForm();
    }
    
	
}
?>